package com.ruoyi.web.controller.baodao.service;

import com.ruoyi.baodao.entity.ImportData1;
import com.ruoyi.baodao.entity.MFGoods9999;
import com.ruoyi.baodao.entity.MFGoods9999Key;
import com.ruoyi.baodao.mapper.BDMapper;
import com.ruoyi.baodao.mapper.MFGoods9999Mapper;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.Md5Utils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class GoodsService {
    @Autowired
    BDMapper bdMapper;
    @Autowired
    MFGoods9999Mapper mfGoods9999Mapper;
    /**
     * 导入药品主治信息
     *
     * @param mfGoods9999s        商品主治信息列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName        操作用户
     * @return 结果
     */
    @DataSource(value = DataSourceType.SLAVE)
    public String importGoodsZZ(List<ImportData1> mfGoods9999s, Boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(mfGoods9999s) || mfGoods9999s.size() == 0)
        {
            throw new BusinessException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
      //  String password = configService.selectConfigByKey("sys.user.initPassword");
        for (ImportData1 mfGoods9999 : mfGoods9999s)
        {
            try
            {
                // 主键
                MFGoods9999Key mfGoods9999Key = new MFGoods9999Key();
                mfGoods9999Key.setGoodscode(mfGoods9999.getGoodscode());
                SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
                String shopId = sysUser.getDept().getDeptName();
              //  mfGoods9999Key.setShopid(sysUser.getDept().getDeptName());
  /*              mfGoods9999Key.setShopid();
                mfGoods9999Key.setGoodscode("101-02-181");*/
               // log.info("查询主键是" + mfGoods9999Key.toString());
                //MFGoods9999 good = mfGoods9999Mapper.selectByPrimaryKey(mfGoods9999Key,"9999");
                String goodsZZ = bdMapper.queryGoodsZZ(shopId,mfGoods9999.getGoodscode());
                log.info("查询主治信息：" + goodsZZ);
                if(goodsZZ == null || goodsZZ.equals("") ){
                    log.info("更新商品主治信息！");
                    mfGoods9999.setGoodsname(null);
                   // mfGoods9999Mapper.updateByPrimaryKeySelective(mfGoods9999,sysUser.getDept().getDeptName());
                   bdMapper.updateGoodsZZ(mfGoods9999.getGoodscode(),mfGoods9999.getGoodsZZ(),shopId);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、商品码" + mfGoods9999.getGoodscode() + " 导入成功");
                }else if(isUpdateSupport){
                    bdMapper.updateGoodsZZ(mfGoods9999.getGoodscode(),mfGoods9999.getGoodsZZ(),shopId);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、商品码" + mfGoods9999.getGoodscode() + " 导入成功");
                }else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、商品码" + mfGoods9999.getGoodscode() + " 已存在");
                }


           /*     // 验证是否存在这个用户
                SysUser u = userMapper.selectUserByLoginName(user.getLoginName());
                if (StringUtils.isNull(u))
                {
                    user.setPassword(Md5Utils.hash(user.getLoginName() + password));
                    user.setCreateBy(operName);
                    this.insertUser(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " + user.getLoginName() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    user.setUpdateBy(operName);
                    this.updateUser(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " + user.getLoginName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、账号 " + user.getLoginName() + " 已存在");
                }*/
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum +"、商品码" + mfGoods9999.getGoodscode()  + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
               // log.error(msg, e);
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new BusinessException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
